import type { QueryOptions } from '@/shared/types';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { notesService } from './notesService';
import { Note, notesServiceKeys } from './domain';

export const notesOptions = (options?: QueryOptions<Note[]>) => ({
    queryKey: notesServiceKeys.notes(),
    queryFn: () => notesService.getNotes(),
    ...options,
});

export const useNotes = (options?: QueryOptions<Note[]>) => {
    return useQuery(notesOptions(options));
};

export const useCreateNote = () => {
    const queryClient = useQueryClient();
    
    return useMutation({
        mutationFn: (data: { content: string }) => notesService.createNote(data),
        onSuccess: () => {
            // Invalidate and refetch notes
            queryClient.invalidateQueries({ queryKey: notesServiceKeys.notes() });
        },
    });
};

export const useDeleteNote = () => {
    const queryClient = useQueryClient();
    
    return useMutation({
        mutationFn: (id: number) => notesService.deleteNote(id),
        onSuccess: () => {
            // Invalidate and refetch notes
            queryClient.invalidateQueries({ queryKey: notesServiceKeys.notes() });
        },
    });
};